#include <gfx/dx12/texture.h>
//#include <system/graphics.h>

primal::gfx::Texture::Texture(const utl::image& img, Graphics* g)
{
	/*

	d3dx::resource_desc rd{};
	rd.as_texture_2d((u64)img.width, (u32)img.height);
	DXASSERT(g->GetDevice()->CreateCommittedResource(&d3dx::heap_properties.default_heap, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(this->GetAddressOf())));
	ComPtr<ID3D12Resource> upload_buffer;
	rd.as_texture_buffer((u64)img.width, (u32)img.height);
	DXASSERT(g->GetDevice()->CreateCommittedResource(&d3dx::heap_properties.upload_heap, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IIDPPV(upload_buffer)));
	
	void* buffer{};
	upload_buffer->Map(0u, nullptr, &buffer);

	if (((img.width * 4) % D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) == 0)
	{
		memcpy(buffer, img.data, (size_t)(img.width * img.height * 4));
	}
	else
	{

	}
	upload_buffer->Unmap(0u, nullptr);

	d3dx::texture_copy_location dst_loc{};
	d3dx::texture_copy_location src_loc{};

	dst_loc.as_texture(ptr_, 0u);
	src_loc.as_buffer(upload_buffer.Get(), (u32)img.width, (u32)img.height);

	auto frame = g->cmd_rc->begin();
	auto list = g->cmd_rc->AcquireNextList(frame);
	list->CopyTextureRegion(&dst_loc, 0u, 0u, 0u, &src_loc, nullptr);

	//list->CopyTextureRegion
	
	d3dx::resource_barrier barrier{};
	barrier.add(ptr_, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
	//barrier.apply(list);

	g->cmd_rc->end(frame, true);

	d3dx::view_desc vd{};

	g->srv->allocator(handle);
	g->GetDevice()->CreateShaderResourceView(ptr_, vd.as_srv_texture_2d(), handle.cpu);

	*/
}
